package com.bank.action;

import java.sql.Timestamp;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.bank.pojo.BankCards;
import com.bank.pojo.BankUserinfo;
import com.bank.svc.BankCardSvc;
import com.bank.svc.BankUserSvc;
import com.bank.tools.BankCardNumGenerator;
import com.bank.tools.EncyptTools;
import com.bank.tools.JSONOutput;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller("bankUserAction")
public class BankUserAction extends ActionSupport{
	@Resource(name="bankUserSvc")
	private BankUserSvc bus;
	@Resource(name="bankCardSvc")
	private BankCardSvc bcs;
	private String name;
	private String udoc;
	private String sex;
	private String uage;
	private String uphone;
	private String umail;
	private String ufrom;
	private String uaddr;
	private String uname;
	private String upwd;
	private String uol;
	private String ucash;
	private String paypwd;
	

	public String addUser() throws Exception{
		String json="";
		BankUserinfo user=new BankUserinfo(name, udoc, Integer.parseInt(sex),Integer.parseInt(uage), uphone, umail, 
											ufrom, uaddr, uname,EncyptTools.MessageEncypt(upwd),
											new Timestamp(new Date().getTime()),Integer.parseInt(uol),1);
		boolean flag=bus.addUser(user);
		if(flag){
			String num=BankCardNumGenerator.generateBankCardNum();
			BankCards card=new BankCards(num,user,EncyptTools.MessageEncypt(paypwd),Float.parseFloat(ucash), 0, 
								Integer.parseInt(uol),new Timestamp(new Date().getTime()));
			bcs.addCard(card);
			num="\""+num.replaceAll("([\\d]{4})", "$1 ")+"\"";
			json="{\"status\":0,\"info\":\"开户完成！\",\"num\":"+num+",\"cash\":"+Float.parseFloat(ucash)+"}";
		}else{
			json="{\"status\":1,\"info\":\"用户名已被占用！\"}";
		}
		JSONOutput.OutputJSON(ServletActionContext.getResponse(), json);
		return "success";
	}
	
	public void setBus(BankUserSvc bus) {
		this.bus = bus;
	}
	public void setBcs(BankCardSvc bcs) {
		this.bcs = bcs;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void setUdoc(String udoc) {
		this.udoc = udoc;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public void setUage(String uage) {
		this.uage = uage;
	}
	public void setUphone(String uphone) {
		this.uphone = uphone;
	}
	public void setUmail(String umail) {
		this.umail = umail;
	}
	public void setUfrom(String ufrom) {
		this.ufrom = ufrom;
	}
	public void setUaddr(String uaddr) {
		this.uaddr = uaddr;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}
	public void setUol(String uol) {
		this.uol = uol;
	}
	public void setUcash(String ucash) {
		this.ucash = ucash;
	}
	public void setPaypwd(String paypwd) {
		this.paypwd = paypwd;
	}
}
